package com.bawei.monthdemo2207a.month01.local

import androidx.compose.foundation.Image
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.AddCircle
import androidx.compose.material.icons.filled.ArrowBack
import androidx.compose.material3.Icon
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.remember
import androidx.compose.runtime.snapshots.SnapshotStateList
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.navigation.NavController
import androidx.navigation.compose.rememberNavController
import com.bawei.monthdemo2207a.R
import com.bawei.monthdemo2207a.month01.route.RouteConfig
import com.blankj.utilcode.util.AppUtils
import com.blankj.utilcode.util.FileUtils
import com.blankj.utilcode.util.ToastUtils
import java.io.File

/**
 * 作者：赵亮
 * 时间：2024/12/27 12:08
 * 功能：
 */
@Composable
fun LocalFilePage(navController: NavController = rememberNavController()) {

    val list = remember {
        SnapshotStateList<File>()
    }

    LaunchedEffect(key1 = "") {
        list.addAll(File("/sdcard/Download").listFiles())
    }

    Column(modifier = Modifier.padding(top = 24.dp)) {

        Row(modifier = Modifier
            .fillMaxWidth()
            .padding(horizontal = 15.dp)) {
            Icon(modifier = Modifier.clickable {
                navController.popBackStack()
            }, imageVector = Icons.Default.ArrowBack, contentDescription = null)
            Text(text = "下载管理", modifier = Modifier.weight(1f))
            Icon(modifier = Modifier.clickable {
                navController.navigate(RouteConfig.LOCAL_APP)
            }, imageVector = Icons.Default.AddCircle, contentDescription = null)
        }

        LazyColumn(modifier = Modifier.padding(horizontal = 15.dp)) {

            items(list) {
                Row {
                    Image(
                        modifier = Modifier.size(30.dp),
                        painter = painterResource(id = R.drawable.logo_weixin),
                        contentDescription = null
                    )
                    Column {
                        Text(text = it.name)
                        Text(text = FileUtils.getSize(it))
                    }
                    Text(text = "安装", modifier = Modifier.clickable {
                        if (it.name.endsWith(".apk")) {
                            AppUtils.installApp(it)
                        } else {
                            ToastUtils.showShort("无法安装")
                        }
                    })
                }
            }
        }
    }
}

@Preview(showSystemUi = true)
@Composable
fun LocalFilePagePreview() {
    LocalFilePage()
}

